package com.mhz.cae.handlers;

import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.shape.Shape;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;

public class TrashableHandler implements IUpdateHandler {

	private final PhysicsWorld physicsWorld;
	private final Scene scene;
	private final Sprite trash;
	private final Shape currentFace;

	public TrashableHandler(final PhysicsWorld physicsWorld, final Scene scene, final Sprite trash, final Shape currentFace) {
		this.physicsWorld = physicsWorld;
		this.scene = scene;
		this.trash = trash;
		this.currentFace = currentFace;
	}

	@Override
	public void onUpdate(float pSecondsElapsed) {
		if (currentFace.collidesWith(trash)) {
			removeObject();
		}
	}

	@Override
	public void reset() {
	}

	public void removeObject() {
		// TODO A voir car provoque une erreur
		// final PhysicsConnector facePhysicsConnector = physicsWorld.getPhysicsConnectorManager().findPhysicsConnectorByShape(currentFace);
		// if (facePhysicsConnector != null) {
		// physicsWorld.unregisterPhysicsConnector(facePhysicsConnector);
		// physicsWorld.destroyBody(facePhysicsConnector.getBody());
		// }
		// if (currentFace != null) {
		// scene.unregisterTouchArea(currentFace);
		// scene.unregisterUpdateHandler(this);
		// scene.detachChild(currentFace);
		// }
		// System.gc();
	}
}
